package com.yoo.ons.function.login

import android.content.Context
import android.content.Intent
import android.view.inputmethod.InputMethodManager
import androidx.core.widget.addTextChangedListener
import com.yoo.chat.kit.utils.StatusBarUtil
import com.yoo.module_base.utils.SaveDeviceUtil
import com.yoo.ons.base.BaseVMActivity
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.databinding.ActivityPhonePwdBinding
import com.yoo.ons.function.MainActivity
import com.yoo.ons.function.register.RegisterActivity
import com.yoo.ons.model.LoginResultModel

class PwdLoginActivity : BaseVMActivity<ActivityPhonePwdBinding, PhoneViewModel>(
    ActivityPhonePwdBinding::inflate,
    PhoneViewModel::class.java
) {
    override fun initView() {
        super.initView()
        binding.titleBar.setBackListener {
            finish()
        }
        binding.titleBar.setTitleBarPadding(StatusBarUtil.getStatusBarHeight(this))
    }

    override fun initData() {

    }

    override fun initListener() {
        super.initListener()

        binding.editPhone.requestFocus()
        // 延迟显示软键盘
        binding.editPhone.postDelayed({
            // 显示软键盘
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(binding.editPhone, InputMethodManager.SHOW_IMPLICIT)
        }, 100)

        binding.editPhone.addTextChangedListener {
            binding.phoneNext.isEnabled = binding.editPhone.text.toString().length == 10
        }

        binding.phoneNext.setOnClickListener {
            // 隐藏软键盘
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(binding.editPhone.windowToken, 0)
            phoneLogin()
        }
    }

    override fun onResume() {
        super.onResume()

    }

    private fun phoneLogin() {
        mViewModel?.getLogin(object : HttpRequestListener<LoginResultModel>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["loginType"] = 1
                params["account"] = "+1${binding.editPhone.text}"
                params["mobile"] = "+1${binding.editPhone.text}"
            }

            override fun success(t: LoginResultModel?) {
                t?.apply {
                    SaveDeviceUtil.saveToken(t.token)
                    SaveDeviceUtil.saveUserCode(t.userCode)
                    SaveDeviceUtil.saveUserValid(t.isUserValid)
                    SaveDeviceUtil.saveAvatarUrl(t.avatarUrl)
                    SaveDeviceUtil.saveDeleteAccount(false)
                    if (t.isUserValid) {
                        MainActivity.start(this@PwdLoginActivity)
                    }else{
                        RegisterActivity.start(this@PwdLoginActivity)
                    }
                    finish()
                }
            }

        })
    }


    companion object {
        fun start(context: Context) {
            val intent = Intent(context, PwdLoginActivity::class.java)
            context.startActivity(intent)
        }
    }
}